//
//  LGFollowRecordHeaderView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/8.
//

#import "LGFollowRecordHeaderView.h"

#import "LGCollectionViewLeftFlowLayout.h"

@interface LGFollowRecordHeaderView()<UICollectionViewDataSource,UICollectionViewDelegate>
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;

@end

@implementation LGFollowRecordHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.numberLabel];
        [self addSubview:self.screenBtn];
        [self addSubview:self.collectionView];
    }
    return self;
}

-(void)setTotalCount:(NSString *)totalCount{
    _totalCount = totalCount;
    if(self.dataArray.count>0){
        self.screenBtn.selected = YES;
        NSString *content = [NSString stringWithFormat:@"筛选出记录 %@ 条",totalCount];
        NSMutableAttributedString *attrbutStr = [[NSMutableAttributedString alloc]initWithString:content];
        [attrbutStr addAttributes:@{NSFontAttributeName:LGFontWeight(15, UIFontWeightMedium),NSForegroundColorAttributeName:[UIColor colorWithString:@"#3291FF"]} range:NSMakeRange(6, totalCount.length)];
        self.numberLabel.attributedText = attrbutStr;
    }else{
        self.screenBtn.selected = NO;
        NSString *content = @" ";
        NSMutableAttributedString *attrbutStr = [[NSMutableAttributedString alloc]initWithString:content];
        [attrbutStr addAttributes:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]} range:NSMakeRange(0, content.length)];
        self.numberLabel.attributedText = attrbutStr;
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.dataArray = [NSMutableArray array];
    NSString *startTime = @"";
    NSString *endTime = @"";
    if(dataDic.allKeys.count>0){
        NSArray *keyArray = dataDic.allKeys;
        for (NSInteger i=0; i<keyArray.count; i++) {
            NSString *key = keyArray[i];
            if([key containsString:@"_start"] || [key containsString:@"_end"]){
                [self.dataArray addObject:@{@"key":key,@"title":LGNSString(dataDic[key])}];
            }else{
                NSDictionary *tempDic = dataDic[key];
                NSArray *subKeys = tempDic.allKeys;
                for (NSInteger j=0; j<subKeys.count; j++) {
                    [self.dataArray addObject:@{@"key":key,@"title":subKeys[j]}];
                }
            }
        }
    }
    self.screenBtn.selected = (self.dataArray.count>0)?YES:NO;
    [self.collectionView reloadData];
    [self performSelector:@selector(refreshViewHeight) withObject:nil afterDelay:0.2];
}

-(void)refreshViewHeight{
    CGFloat height = self.collectionView.contentSize.height;
    self.collectionView.frame = CGRectMake(0, viewPix(45), Screen_W, height);
    if(self.delegate && [self.delegate respondsToSelector:@selector(headViewHeightChanged:)]){
        [self.delegate headViewHeightChanged:height+viewPix(50)];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGFollowRecordScreenItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.content = LGNSString(self.dataArray[indexPath.item][@"title"]);
    cell.index = indexPath.item;
    __weak typeof(self) weakSelf = self;
    cell.deleteItemAtIndex = ^(NSInteger index) {
        if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(deleteScreenItem:)]){
            [weakSelf.delegate deleteScreenItem:weakSelf.dataArray[index]];
        }
//        [weakSelf.dataArray removeObjectAtIndex:index];
//        [weakSelf.collectionView reloadData];
    };
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *content = LGNSString(self.dataArray[indexPath.item][@"title"]);
    CGFloat width = [content textWidthWithFont:LGFontWeight(15, UIFontWeightMedium) maxHeight:MAXFLOAT]+viewPix(40);
    return CGSizeMake(width+2, viewPix(28));
}


-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
        _numberLabel.frame = CGRectMake(viewPix(20), viewPix(15), viewPix(150), viewPix(21));
    }
    return _numberLabel;
}


-(UIButton *)screenBtn{
    if(!_screenBtn){
        _screenBtn = [UIButton buttonWithTitle:@"筛选" titleFont:LGFont(15) normalColor:@"#666666" selectColor:@"#4C87FD" normalImage:@"screenGray12" selectImage:@"screenBlue12"];
        _screenBtn.frame = CGRectMake(Screen_W-viewPix(66), viewPix(10), viewPix(50), viewPix(33));
        _screenBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_screenBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
    }
    return _screenBtn;
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        LGCollectionViewLeftFlowLayout *layout = [[LGCollectionViewLeftFlowLayout alloc]init];
        layout.minimumInteritemSpacing = viewPix(8);
        layout.minimumLineSpacing = viewPix(8);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, viewPix(45), Screen_W, 0) collectionViewLayout:layout];
        [_collectionView registerClass:[LGFollowRecordScreenItem class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.contentInset = UIEdgeInsetsMake(0, viewPix(16), 0, viewPix(10));
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.scrollEnabled = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}



@end

#pragma mark
#pragma mark ====> LGPatientScreenItem

@interface LGFollowRecordScreenItem()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIButton *closeButton;

@end

@implementation LGFollowRecordScreenItem

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.baseView];
        [self.baseView addSubview:self.contentLabel];
        [self.baseView addSubview:self.closeButton];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.centerY.equalTo(self.contentView);
            make.height.equalTo(@(viewPix(28)));
        }];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.baseView).offset(viewPix(12));
            make.right.equalTo(self.baseView).offset(-viewPix(28));
            make.centerY.equalTo(self.baseView);
        }];
        [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self.baseView);
            make.width.equalTo(@(viewPix(28)));
        }];
    }
    return self;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.contentLabel.text = content;
}

-(void)closeButtonAction{
    if(self.deleteItemAtIndex){
        self.deleteItemAtIndex(self.index);
    }
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] init];
        _baseView.backgroundColor = [UIColor colorWithString:@"#EBF3F8"];
        _baseView.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
        _baseView.layer.borderWidth = 1;
        _baseView.cornerRidus = viewPix(14);
    }
    return _baseView;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _contentLabel;
}

-(UIButton *)closeButton{
    if(!_closeButton){
        _closeButton = [UIButton buttonWithImage:@"closeGray12" selectImage:@"closeGray12"];
        [_closeButton addTarget:self action:@selector(closeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}


@end
